package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.zm.ce.domain.ZMCEMaintenanceApply;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkPlan;

/**
 * <p>Title: ServiceZMCE42.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author  高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-19 下午02:40:20  高强  Create
 *</pre>
 */
public class ServiceZMCE42 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = query(inInfo);
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.empName", "empCode", "empName", true));//人员
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		return outInfo;
	}

	/**
	 * 维修申请查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCEMaintenanceApply.query", new ZMCEMaintenanceApply());				
		return outInfo;
	}
	
	/**
	 * 工作计划新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validateValue = validate(inInfo, "ZMCEWorkPlan.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			return insertAgain(inInfo);
		}
	}
	
	/**
	 * 工作计划新增
	 * <p>Function: insertAgain</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	public EiInfo insertAgain(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "keyID", inInfo.getBlock("result").getCellStr(0, "MaintenanceApplykeyID")); 
		EiInfo outInfo =super.update(inInfo, "ZMCEMaintenanceApply.modifyPlan");//修改状态为计划
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_WORKPLAN"));//主键
		inInfo.getBlock("result").setCell(0, "fdIsExtension", "否");
		inInfo.getBlock("result").setCell(0, "fdUseDepartment", Department.getInstance().getDepartmentNameById(inInfo.getBlock("result").getCellStr(0, "fdUseDepartmentId")));
		inInfo.getBlock("result").setCell(0, "fdPreparer", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdPreparerId", UserSession.getUserId());
		inInfo.getBlock("result").setCell(0, "fdPreparerDepId", UserSession.getInSessionProperty( "orgCode" ));
		if("计划".equals(inInfo.getBlock("result").getCell(0, "fdStatus")))
		{
			inInfo.getBlock("result").setCell(0, "fdTrueBeganDate", StaticTools.getDate());
			if("大修".equals(inInfo.getBlock("result").getCell(0, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(0, "fdType")))
			{
				if(Equipment.updateEquipmentStatus("待修", "不完好", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
				{
					inInfo.setStatus(-1);
					return inInfo;
				}
			}
		}
		outInfo = super.insert(inInfo, "ZMCEWorkPlan.insert");
		return outInfo;
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdInvalidPerson", inInfo.getBlock("result").getCellStr(0, "fdPerson"));
		inInfo.getBlock("result").setCell(0, "fdInvalidTime", StaticTools.getDateTime());
		EiInfo outInfo = super.update(inInfo, "ZMCEMaintenanceApply.modifyStop");
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: updateAgain</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:54:00 张小飞
	 * </p>
	 *
	 */
	public EiInfo updateAgain(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdIsExtension", "否");
		inInfo.getBlock("result").setCell(0, "fdModifier", UserSession.getInSessionProperty("UserCname"));
		
		EiInfo outInfo = super.update(inInfo, "ZMCEWorkPlan.update");
		return outInfo;
		
	}
	
	/**
	 * 编号重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-14下午01:08:21 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		String fdEquipmentId = inInfo.getBlock("result").getCellStr(0, "fdEquipmentId");
		String fdBeganDate = inInfo.getBlock("result").getCellStr(0, "fdBeganDate");
		String fdEndDate = inInfo.getBlock("result").getCellStr(0, "fdEndDate");
		
		params.put("fdType", fdType);
		params.put("fdEquipmentId", fdEquipmentId);
		params.put("fdBeganDate", fdBeganDate);
		params.put("fdEndDate", fdEndDate);
		
		Map<String, String> param = new HashMap<String, String>();
		param.put("fdCode", inInfo.getBlock("result").getCellStr(0, "fdCode"));
		List result1 = dao.query("ZMCEWorkPlan.validateForMaintenanceApply", param);
		if(result1!=null && result1.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.set("mark", "no");
			inInfo.setMsg("计划编号有重复,不能提交");
			return true;
		}
		
		params.put("fdStatus", "作废");
		List<ZMCEWorkPlan> result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.set("mark", "no");
			inInfo.setMsg("该计划时间里已经存在该设备的"+fdType+"工作计划，查看"+result.get(0).getFdCode()+"计划！");
			return true;
		}
		
		params.remove("fdType");
		List<ZMCEWorkPlan> resultNoType = dao.query(sqlName, params);
		if(resultNoType != null && resultNoType.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.set("mark", "may");
			inInfo.setMsg("该计划与"+resultNoType.get(0).getFdCode()+"计划的时间段重叠，确定要提交？");
			return true;
		}
		return false;
	}
	
	

}
